﻿using System.Text;
using System.Text.Json;

namespace OpenHunauEleEmailSender
{
	public class Config
	{
		public const string DefConfigFile = "./Config.json";
		private static Config? inst;
		public static Config Inst { get => inst!; }
		public Config()
		{
			EleLightingsGetterToken = "";
			EleAirConditionerGetterToken = "";
			RecipientsEmails = "excample@email.com,excample@email.com,excample@email.com";
			SenderEmailAddress = "excample@163.com";
			SenderEmailPassword = "PASSWORD";
			SenderEmailServerHost = "smtp.163.com";
			SenderEmailServerPort = 25;
		}
		public static bool LoadConfig()
		{
			try
			{
				string json = File.ReadAllText(DefConfigFile, Encoding.UTF8);
				inst = JsonSerializer.Deserialize(json, AppJsonSerializerContext.Default.Config);
				if (inst is null)
					return false;
				try
				{
					inst.EleLightingsGetterToken = inst.EleLightingsGetterToken.Trim();
					inst.EleAirConditionerGetterToken = inst.EleAirConditionerGetterToken.Trim();
				}
				catch
				{
					CreateConfig();
					return false;
				}
				return true;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				return false;
			}
		}
		public static void CreateConfig()
		{
			inst ??= new();
			string json = JsonSerializer.Serialize(inst, AppJsonSerializerContext.Default.Config);
			File.WriteAllText(DefConfigFile, json, Encoding.UTF8);
		}

		//在查询平台的创建二维码页面最底下会生成此页的自动化Token，填入即可，置空表示关闭此项查询。
		public string EleLightingsGetterToken { get; set; }
		public string EleAirConditionerGetterToken { get; set; }
		//设置若干收件人的的邮箱
		public string RecipientsEmails { get; set; }
		//设置发件人的邮箱
		public string SenderEmailAddress { get; set; }
		//设置发件人的授权码。相当于登录邮箱的密码。从邮箱提供的服务商获取。
		public string SenderEmailPassword { get; set; }
		//设置smtp的服务地址，这里以163邮箱为例
		public string SenderEmailServerHost { get; set; }
		//设置邮箱使用的端口，这里以163邮箱为例
		public int SenderEmailServerPort { get; set; }
	}
}
